const state = {
  isCol: false,
  tabs: []
}

const mutations = {
  toggleAside(state) {
    state.isCol = !state.isCol
  },
  addTabs(state, payload) {
    if (
      state.tabs.every(v => {
        return v.name !== payload.name
      })
    ) {
      state.tabs.push(payload)
    }
  },
  removeTabs(state, payload) {
    state.tabs = state.tabs.filter(v => v.name !== payload)
  }
}

const actions = {
  toggleAside({ commit }) {
    commit('toggleAside')
  },
  addTabs({ commit }, payload) {
    commit('addTabs', payload)
  },
  removeTabs({ commit, state }, payload) {
    let index = state.tabs.findIndex(v => v.name === payload)
    commit('removeTabs', payload)
    if (index >= state.tabs.length) {
      index--
    }
    return Promise.resolve(index)
  }
}

export default { namespaced: true, state, mutations, actions }
